package com.sprouting.ops.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.ops.dao.*;
import com.sprouting.ops.model.*;
import com.sprouting.ops.model.qo.OpsSiteQo;
import com.sprouting.ops.model.vo.OpsComputerServerVo;
import com.sprouting.ops.model.vo.OpsSiteVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 现场
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2023/11/8 14:44
 */
@Service
@Slf4j
public class OpsSiteService extends ServiceImpl<OpsSiteDao, OpsSite> {

    @Resource
    private OpsSiteDao opsSiteDao;
    @Resource
    private DromaraFileStorageDao dromaraFileStorageDao;
    @Resource
    private OpsStakeholderDao opsStakeholderDao;
    @Resource
    private OpsSiteTimeLineDao opsSiteTimeLineDao;
    @Resource
    private OpsThirdPartySiteUserDao opsThirdPartySiteUserDao;
    @Resource
    private OpsEnvAccountDao opsEnvAccountDao;
    @Resource
    private OpsEnvDao opsEnvDao;
    @Resource
    private OpsSiteNetworkDao opsSiteNetworkDao;
    @Resource
    private OpsComputerServerDao opsComputerServerDao;
    @Resource
    private OpsComputerFileDao opsComputerFileDao;
    @Resource
    private OpsDeployToolDao opsDeployToolDao;
    @Resource
    private OpsComputerServerService opsComputerServerService;
    @Resource
    private DromaraFileStorageService dromaraFileStorageService;
    @Resource
    private OpsQuickAccessDao opsQuickAccessDao;


    /**
     * 根据名称查询现场，过滤掉指定id
     * @param name 现场名称
     * @param id 现场id，会被过滤
     * @return
     */
    public OpsSite getByNameFilterId(String name, String id) {
        return opsSiteDao.getByNameFilterId(name, id);
    }

    /**
     * 分页查询现场信息
     * @param page 分页参数
     * @param query 查询参数
     * @return
     */
    public Page<OpsSiteVo> getSitePageList(Page page, OpsSiteQo query) {
        return opsSiteDao.getSitePageList(page, query);
    }

    /**
     * 保存场所
     * @param opsSite 场所信息
     * @param opsUploadFile 上传的图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveSite(OpsSite opsSite, DromaraFileStorage opsUploadFile) {
        opsSiteDao.insert(opsSite);
        if (opsUploadFile != null){
            dromaraFileStorageDao.updateById(opsUploadFile);
        }
    }

    /**
     * 更新场所
     * @param opsSite 场所信息
     * @param uploadFileArray 数组下标0， 新的  数组下标1 旧的文件
     */
    @Transactional(rollbackFor = Exception.class)
    public void updSite(OpsSite opsSite, DromaraFileStorage[] uploadFileArray) {
        opsSiteDao.updateById(opsSite);

        if (uploadFileArray[0] != null){
            // 新的
            dromaraFileStorageDao.updateById(uploadFileArray[0]);
        }
        if (uploadFileArray[1] != null){
            // 旧的文件
            dromaraFileStorageDao.updateById(uploadFileArray[1]);
        }
    }

    /**
     * 删除场所
     * @param site 场所信息
     * @param opsUploadFile 上传的图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void delSite(OpsSite site, DromaraFileStorage opsUploadFile) {
        opsSiteDao.deleteById(site);

        if (opsUploadFile != null){
            dromaraFileStorageDao.updateById(opsUploadFile);
        }

        // 干系人
        LambdaUpdateWrapper<OpsStakeholder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(OpsStakeholder::getSiteId, site.getId());
        opsStakeholderDao.delete(wrapper);

        // 时间轴
        LambdaUpdateWrapper<OpsSiteTimeLine> wrapperSiteTimeLine = new LambdaUpdateWrapper<>();
        wrapperSiteTimeLine.eq(OpsSiteTimeLine::getSiteId, site.getId());
        opsSiteTimeLineDao.delete(wrapperSiteTimeLine);

        // 厂商人员
        LambdaUpdateWrapper<OpsThirdPartySiteUser> wrapperThirdPartySiteUser = new LambdaUpdateWrapper<>();
        wrapperThirdPartySiteUser.eq(OpsThirdPartySiteUser::getSiteId, site.getId());
        opsThirdPartySiteUserDao.delete(wrapperThirdPartySiteUser);

        // 环境账号
        LambdaUpdateWrapper<OpsEnvAccount> wrapperEnvAccount = new LambdaUpdateWrapper<>();
        wrapperEnvAccount.eq(OpsEnvAccount::getSiteId, site.getId());
        opsEnvAccountDao.delete(wrapperEnvAccount);

        // 环境
        LambdaUpdateWrapper<OpsEnv> wrapperEnv = new LambdaUpdateWrapper<>();
        wrapperEnv.eq(OpsEnv::getSiteId, site.getId());
        opsEnvDao.delete(wrapperEnv);

        // 网络拓扑
        LambdaUpdateWrapper<OpsSiteNetwork> wrapperSiteNetwork = new LambdaUpdateWrapper<>();
        wrapperSiteNetwork.eq(OpsSiteNetwork::getSiteId, site.getId());
        opsSiteNetworkDao.delete(wrapperSiteNetwork);

        // 删除文件树
        LambdaUpdateWrapper<OpsComputerFile> wrapperComputerFile = new LambdaUpdateWrapper<>();
        wrapperComputerFile.eq(OpsComputerFile::getSiteId, site.getId());
        opsComputerFileDao.delete(wrapperComputerFile);

        // 绑定的工具
        LambdaUpdateWrapper<OpsDeployTool> wrapperDeployTool = new LambdaUpdateWrapper<>();
        wrapperDeployTool.eq(OpsDeployTool::getSiteId, site.getId());
        opsDeployToolDao.delete(wrapperDeployTool);
        // todo 工具对应的图片没处理

        // 计算机
        List<OpsComputerServerVo> computerServerVoList = opsComputerServerDao.getBySiteId(site.getId());
        LambdaUpdateWrapper<OpsComputerServer> wrapperComputerServer = new LambdaUpdateWrapper<>();
        wrapperComputerServer.eq(OpsComputerServer::getSiteId, site.getId());
        opsComputerServerDao.delete(wrapperComputerServer);

        // 删除组件之类的关联关系--删除计算机
        if (CollUtil.isNotEmpty(computerServerVoList)){
            for (OpsComputerServerVo opsComputerServerVo : computerServerVoList) {
                List<DromaraFileStorage> accessList = dromaraFileStorageService.getByBindId(opsComputerServerVo.getId());
                OpsComputerServer opsComputerServer = BeanUtil.copyProperties(opsComputerServerVo, OpsComputerServer.class);
                opsComputerServerService.delComputerServer(opsComputerServer, accessList);
            }
        }

        // 删除快捷方式
        LambdaUpdateWrapper<OpsQuickAccess> wrapperQuickAccess = new LambdaUpdateWrapper<>();
        wrapperQuickAccess.eq(OpsQuickAccess::getBindId, site.getId());
        opsQuickAccessDao.delete(wrapperQuickAccess);
    }

    /**
     * 根据场所ID查询
     * @param siteId 场所ID
     * @return
     */
    public OpsSiteVo getSiteById(String siteId) {
        return opsSiteDao.getSiteById(siteId);
    }

    /**
     * 获取可添加的场所
     * @param userId 当前用户id
     * @return
     */
    public List<OpsSite> getByQuickSite(String userId) {
        return opsSiteDao.getByQuickSite(userId);
    }

}
